package random.fox.p2p.ui.customview;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import random.fox.p2p.R;
import random.fox.p2p.bean.Dynamic;

/**
 * Created by acer on 2015/3/22.
 */
public class DynamicTimeLineAdapter extends BaseAdapter {
    protected ImageLoader imageLoader = ImageLoader.getInstance();
    DisplayImageOptions options;
    private Context context ;
    private List<Dynamic> list;

    private int day ,month; //记录上一项的日期
    public DynamicTimeLineAdapter(List<Dynamic> list){
        this.list = list;
        // 使用DisplayImageOptions.Builder()创建DisplayImageOptions
        options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)  // 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher)       // 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)                        // 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)                          // 设置下载的图片是否缓存在SD卡中
                .build();
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(context==null)
            context = parent.getContext();
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context , R.layout.dynamic_time_line_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        Dynamic dynamic = list.get(position);
        Date date = dynamic.getDate();
        Calendar calendar  =  Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        if(day ==calendar.get(Calendar.DATE) && month == calendar.get(Calendar.MONTH)+1){
            holder.dynamicDayText.setText("");
            holder.dynamicMonthText.setText("");
        }else {
            holder.dynamicDayText.setText(calendar.get(Calendar.DATE) + "");
            holder.dynamicMonthText.setText(calendar.get(Calendar.MONTH) + 1 + "月");
        }
        day = calendar.get(Calendar.DATE);
        month = calendar.get(Calendar.MONTH) + 1;
        holder.dynamicTitleText.setText(dynamic.getTitle());

        imageLoader.displayImage(dynamic.getUrls().get(0) , holder.dynamicImageView , options);

        return convertView;
    }

    class ViewHolder {
        @InjectView(R.id.dynamic_time_line_img)
        ImageView dynamicImageView;
        @InjectView(R.id.dynamic_time_line_day)
        TextView dynamicDayText;
        @InjectView(R.id.dynamic_time_line_month)
        TextView dynamicMonthText;
        @InjectView(R.id.dynamic_time_line_title)
        TextView dynamicTitleText;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
